<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@page import="conexion.KAUMProperties"%>

<%@page import="html.*"%>
<%@page import="model.*"%>
<%@page import="controller.*"%>
<%@page import="conexion.*"%>
<%@page import="users.model.*"%>
<%@page import="users.controller.*"%>
<%@page import="portlets.model.*"%>
<%@page import="portlets.controller.*"%>
<%@page import="directory.model.*"%>
<%@page import="directory.controller.*"%>
<%@page import="schema.model.*"%>
<%@page import="schema.controller.*"%>
<%@page import="java.util.Vector"%>
<%@page import="java.text.DecimalFormat"%>

<%@page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@page import="java.util.GregorianCalendar"%>
<%@ page import="com.oreilly.servlet.MultipartRequest"%>



<%
            response.addHeader("P3P", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
            request.setCharacterEncoding("ISO-8859-1");
            if (session == null || session.getId() == null) {
                response.sendRedirect(KAUMProperties.SERVER_URL + "index.jsp?IDX=1");
            }


            Usuario m_user = (Usuario) session.getAttribute("CURRENT_USER");

            DecimalFormat m_decFormat = new DecimalFormat("#,###");

            String m_IDX = (String) session.getAttribute("CURRENT_IDX");
            String m_IDP = (String) session.getAttribute("CURRENT_IDP");

            DBConnectionPool.setConnections(DbSetup.dbSetup.getConnections());

            String IDX = request.getParameter("IDX");
            if (IDX == null) {
                IDX = m_IDX;
                if (IDX == null) {
                    IDX = "1";
                }
            }
            session.setAttribute("CURRENT_IDX", IDX);

            String IDP = request.getParameter("IDP");
            if (IDP == null) {
                IDP = m_IDP;
                if (IDP == null) {
                    IDP = "0";
                }
            }
            session.setAttribute("CURRENT_IDP", IDP);

            String IDS = request.getParameter("IDS");
            if (IDS == null) {
                IDS = "-1";
            }
            if (m_user == null) {
                String autorizado[] = ParametroMgr.mgr.getIdColumn("select count(*) from MainMenu where id=" + IDX + " and (publica=1 or publica=2 )");
                if (autorizado[0].equals("0")) {
                    response.sendRedirect(KAUMProperties.SERVER_URL + "index.jsp?IDX=1");

                }
            } else {
                Vector<Parametro> lst = ParametroMgr.mgr.getListFromQuery("select idPersonRelationType, 0 from PersonRelationship where idPersonB=" + m_user.id);

                String szp = "0, ";
                for (Parametro it : lst) {
                    szp += it.id + ", ";
                }
                szp = szp.substring(0, szp.length() - 2);
                String autorizado[] = ParametroMgr.mgr.getIdColumn("select count(*) from MainMenu where id=" + IDX + " and ( (publica=0 and perfil in ( " + szp + " ) ) or publica=2 )");
                if (autorizado[0].equals("0")) {
                    response.sendRedirect("index.jsp?IDX=1");
                }
            }
            String std = request.getParameter("STD");
            if (std == null) {
                std = (String) session.getAttribute("MENUS_STD");
                if (std == null) {
                    std = "0";
                }
            } else {
                session.setAttribute("MENUS_STD", std);
            }

            String opt = request.getParameter("opt");
            if (opt == null) {
                opt = (String) session.getAttribute("MENUS_OPT");
                if (opt == null) {
                    opt = "-1";
                }
            } else {
                session.setAttribute("MENUS_OPT", opt);
            }

            if (!IDS.equals("-1") && !IDP.equals("0")) {
                String szp[] = ParametroMgr.mgr.getIdColumn("select id from PortletTab where idPortlet='" + IDP + "' and estado=" + std + " order by idx");
                if (szp == null || szp.length == 0) {
                } else {
                    opt = szp[0];
                    session.setAttribute("MENUS_OPT", opt);
                }
            }



            Portlet portlet = PortletMgr.mgr.getItem(IDX, IDS, IDP, m_user, opt, std);
            if (opt.equals("-1")) {
                opt = portlet.m_opt;
            }
%>